﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.BinaryData{
	[System.Web.UI.ValidationProperty("ValidationText")]
	public partial class DropDownListBinaryData : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				 BindBinaryData();
			}
		}
		public event EventHandler SelectedIndexChanged;
		protected void drd_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (SelectedIndexChanged != null)
			{
				SelectedIndexChanged(sender, e);
			}
		}
		public bool AppendDataBoundItems
		{
			get
			{
				return drd.AppendDataBoundItems;
			}
			set
			{
				drd.AppendDataBoundItems = value;
			}
		}
		public string AccessKey
		{
			get
			{
				return drd.AccessKey;
			}
			set
			{
				drd.AccessKey = value;
			}
		}
		public System.Web.UI.AttributeCollection Attributes
		{
			get
			{
				return drd.Attributes;
			}
		}
		public bool AutoPostBack
		{
			get
			{
				return drd.AutoPostBack;
			}
			set
			{
				drd.AutoPostBack = value;
			}
		}
		public Color BackColor
		{
			get
			{
				return drd.BackColor;
			}
			set
			{
				drd.BackColor = value;
			}
		}
		public Color BorderColor
		{
			get
			{
				return drd.BorderColor;
			}
			set
			{
				drd.BorderColor = value;
			}
		}
		public System.Web.UI.WebControls.BorderStyle BorderStyle
		{
			get
			{
				return drd.BorderStyle;
			}
			set
			{
				drd.BorderStyle = value;
			}
		}
		public string CssClass
		{
			get
			{
				return drd.CssClass;
			}
			set
			{
				drd.CssClass = value;
			}
		}
		public string DataMember
		{
			get
			{
				return drd.DataMember;
			}
			set
			{
				drd.DataMember = value;
			}
		}
		public object DataSource
		{
			get
			{
				return drd.DataSource;
			}
			set
			{
				drd.DataSource = value;
			}
		}
		public string DataSourceID
		{
			get
			{
				return drd.DataSourceID;
			}
			set
			{
				drd.DataSourceID = value;
			}
		}
		public string DataTextField
		{
			get
			{
				return drd.DataTextField;
			}
			set
			{
				drd.DataTextField = value;
			}
		}
		public string DataTextFormatString
		{
			get
			{
				return drd.DataTextFormatString;
			}
			set
			{
				drd.DataTextFormatString = value;
			}
		}
		public string DataValueField
		{
			get
			{
				return drd.DataValueField;
			}
			set
			{
				drd.DataValueField = value;
			}
		}
		private bool _IsAddChooseItem=false;
		private bool _IsAddAllItem=false;
		private string _SelectedValue="";
		public bool IsAddChooseItem{
			set{
				_IsAddChooseItem=value;
			}
			get{
				return _IsAddChooseItem;
			}
		}
		public bool IsAddAllItem{
			set{
				_IsAddAllItem=value;
			}
			get{
				return _IsAddAllItem;
			}
		}
		Dictionary<string, object> searchValues = new Dictionary<string, object>();
		public void AddSearchValues(string FieldName, object value)
		{
			searchValues.Add(FieldName, value);
		}
		private List<BinaryDataEntity> FilterList(List<BinaryDataEntity> ls)
		{
			if(ls==null)return new List<BinaryDataEntity>();
			if (searchValues.Count == 0) return ls;
			if (ls.Count == 0) return ls;
			System.Reflection.PropertyInfo[] fields = ls[0].GetType().GetProperties();
			return ls.FindAll(delegate(BinaryDataEntity oFind)
			{
				foreach (string key in searchValues.Keys)
				{
					foreach (System.Reflection.PropertyInfo f in fields)
					{
						if (key.ToLower() == f.Name.ToLower())
						{
							if (f.GetValue(oFind,null).ToString().Equals(searchValues[key].ToString()))
								return true;
						}
					}
				}
				return false;
			}
			);
		}
		//Bind data to txtBinaryData
		public void BindBinaryData(){
			BinaryDataController.LoadAllBinaryData(false);
			List<BinaryDataEntity>lsBinaryData = BinaryDataController.ListBinaryDataStatic;
			if(lsBinaryData!=null)
				lsBinaryData=new List<BinaryDataEntity>(lsBinaryData);
			lsBinaryData=FilterList(lsBinaryData);
			if(IsAddChooseItem){
				BinaryDataEntity eChooseItem = new BinaryDataEntity();
				eChooseItem.FileName = Constants.CHOOSE_ITEM;
				lsBinaryData.Insert(0,eChooseItem);
			}		
			if(IsAddAllItem){
				BinaryDataEntity eChooseItem = new BinaryDataEntity();
				eChooseItem.FileName = Constants.APP_ALL;
				lsBinaryData.Insert(0,eChooseItem);
			}			
			this.drd.DataTextField = "FileName";
			this.drd.SelectedValue=null;
			this.drd.DataValueField = "BinaryDataId";
			this.drd.DataSource = lsBinaryData;
			this.drd.DataBind();
			if(_SelectedValue==""){
				if(this.drd.Items.Count>0){
					this.drd.SelectedIndex=0;
				}
			}
			else{
				if (drd.Items.FindByValue(_SelectedValue) != null)
				{
					this.drd.SelectedValue = _SelectedValue;
				}
				else{
					if(this.drd.Items.Count>0){
						this.drd.SelectedIndex=0;
					}
				}
			}
		}
		public string SelectedValue{
			get{
				return drd.SelectedValue;
			}
			set{
				_SelectedValue=value;
				drd.SelectedValue=value;
			}
		}
		public int SelectedIndex{
			get{
				return drd.SelectedIndex;
			}
			set{
				drd.SelectedIndex=value;
			}
		}
		[System.ComponentModel.Browsable(false)]
		public string ValidationText
		{
			get
			{
				if (_IsAddChooseItem)
				{
					if (drd.SelectedIndex <= 0)
					{
						return string.Empty;
					}
				}
				else
				{
					if (drd.SelectedIndex < 0) return string.Empty;
				}
				return drd.Text;
			}
		}
		public System.Web.UI.WebControls.Unit Width
		{
			get
			{
				return drd.Width;
			}
			set
			{
				drd.Width = value;
			}
		}
	}
}
